package com.ifcreate.infowell.generator;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.ifcreate.infowell.common.util.UUIDUtils;
import com.ifcreate.infowell.entity.InfoWellNode;
import com.ifcreate.infowell.sys.service.FileUploadService;
import lombok.Data;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import cn.hutool.core.codec.Base64;

import java.io.File;
import java.net.URL;
import java.util.*;
import java.util.concurrent.Future;

/**
 * @Author chenzedeng
 * @Email yustart@foxmail.com
 * @Create 2020-10-29 4:35 下午
 */
public class Main {

    public static final Map<Integer, ListUrlParse> typeListUrl = MapUtil.newHashMap(5);


    //初始化分类的Url
    static {
        typeListUrl.put(1, new ListUrlParse("http://www.fuzhong.sd.cn/xwdt/fzyw.htm", 183, false));
        typeListUrl.put(2, new ListUrlParse("http://www.fuzhong.sd.cn/zsw1/zsdt.htm", 14, false));
        typeListUrl.put(3, new ListUrlParse("http://www.fuzhong.sd.cn/djw/gzdt.htm", 15, false));
        typeListUrl.put(4, new ListUrlParse("http://www.fuzhong.sd.cn/xwdt/zhxw.htm", 3, false));
        typeListUrl.put(5, new ListUrlParse("http://www.fuzhong.sd.cn/xwdt/btxw.htm", 272, false));
    }

    public static void main(String[] args) throws Exception {
        start();
    }


    public static Set<InfoWellNode> start() throws Exception {
        Set<InfoWellNode> nodeList = new HashSet<>();
        List<Future<Set<InfoWellNode>>> futures = new ArrayList<>();

        for (Map.Entry<Integer, ListUrlParse> entry : typeListUrl.entrySet()) {
            //最大爬取的页码
            int maxCount = 3;
            //开始的页码
            int startPage = 0;
            //结束的页码
            int endPage = entry.getValue().getStartIndex() < maxCount ? entry.getValue().getStartIndex() : maxCount;

            ListUrlParse urlParse = entry.getValue();
            for (int i = startPage; i <= endPage; i++) {
                int page = i;

                futures.add(ThreadUtil.execAsync(() -> {
                    Set<InfoWellNode> list = new HashSet<>();

                    try {
                        Document body = Jsoup.parse(new URL(urlParse.getNextPage(page)), 10000);
                        Elements items = body.getElementsByClass("g_c1pt1nrb clearfix");
                        for (Element item : items) {
                            String href = item.select("a").get(0).attr("href");
                            body = Jsoup.parse(new URL(getDetailUrl(href)), 15000);
                            InfoWellNode node = new InfoWellNode();
                            String title = body.select(".g_c3pt1tle").get(0).text();
                            node.setName(title);
                            node.setTitle(title);

                            //是否是大节点
                            node.setIsNews(true);
                            //节点类型
                            node.setType(entry.getKey());

                            List<String> images = new ArrayList<>();
                            Element content = body.getElementsByClass("v_news_content").get(0);

                            //查询撰稿人
                            Optional<Element> author = content.select("p").stream().filter(c -> c.text().contains("撰稿")).findFirst();
                            if (author.isPresent()) {
                                node.setInfoProvider(author.get().text());
                                author.get().remove();
                            }

                            for (Element img : content.select("img")) {
                                String src = img.attr("src");
                                String srcUrl = getImageSrcUrl(src);
                                //将这个图片下载到本地转换为Base64 - ()
                                FileUploadService.UploadResult uploadResult = downloadImage(srcUrl);
//                                String imageBase64 = Base64.encode(new File(uploadResult.getMediaId()));
//                                img.attr("src", StrUtil.format("data:image/jpeg;base64,{}", imageBase64));
                                img.attr("src", srcUrl);

                                images.add(uploadResult.getUrl());
                                img.remove();
                            }
                            node.setInfoContent(Base64.encode(content.html()));
                            node.setImages(JSONUtil.toJsonStr(images));
                            node.setClickCount(0L);

                            //初始化时间
                            Optional<Element> optional = body.select(".g_b3topinfl1wz").stream().filter(k -> k.text().contains("日期")).findFirst();
                            if (optional.isPresent()) {
                                String originText = optional.get().text();
                                String dateStr = originText.substring(originText.indexOf("2")).trim();
                                node.setTime(DateUtil.parse(dateStr, "yyyy-MM-dd").getTime());
                            }
                            list.add(node);

                            Console.log("Success =====>>> Name:{} Title:{} Provider:{}", node.getName(), node.getTitle(), node.getInfoProvider());
                        }
                    } catch (Exception e) {
                        Console.error(e);
                    }

                    return list;
                }));
            }
        }

        for (Future<Set<InfoWellNode>> future : futures) {
            nodeList.addAll(future.get());
        }

        System.out.println("执行完毕========> " + nodeList.size());

        return nodeList;
    }


    public static String getDetailUrl(String path) {
        int infoOf = path.indexOf("info");
        return StrUtil.format("http://www.fuzhong.sd.cn/{}", path.substring(infoOf));
    }

    public static String getImageSrcUrl(String src) {
        return "http://www.fuzhong.sd.cn" + src;
    }


    /**
     * 下载图片到本地
     * @param path
     * @return
     */
    public static FileUploadService.UploadResult downloadImage(String path) {
        //存储的文件夹
        String prefix = "image";
        //存储的路径
        String filePath = "C:\\Users\\wentingjie\\workspace\\other\\img";
        FileUploadService.UploadResult result = new FileUploadService.UploadResult();
        File file = new File(filePath, UUIDUtils.generateShortUuid() + ".jpg");
        HttpUtil.downloadFile(path, file);
        result.setUrl(StrUtil.format("{}/{}", prefix, file.getName()));
        result.setMediaId(file.getAbsolutePath());
        return result;
    }

    @Data
    private static class ListUrlParse {

        private String url;

        private Integer startIndex;

        private boolean isAddNext;

        public ListUrlParse(String url, Integer startIndex, boolean isAddNext) {
            this.url = url;
            this.startIndex = startIndex;
            this.isAddNext = isAddNext;
        }

        /**
         * 获取下一页
         * @param page
         * @return
         */
        public String getNextPage(int page) {
            if (page == 0) {
                return this.url;
            }
            if (this.isAddNext) {
                page = startIndex + (page - 1);
            } else {
                page = startIndex - (page - 1);
            }
            int lastIndexOf = this.url.lastIndexOf(".");
            return StrUtil.format("{}/{}.htm", this.url.substring(0, lastIndexOf), page);
        }
    }
}
